<?php
global $options;

foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}

$dateformat = get_option('date_format');
$timeformat = get_option('time_format');

$currCat = get_the_category();
$parent = get_cat_name($currCat[0]->category_parent);

if ( is_category( 'individual-projects' ) ) {
	$cat_id = get_cat_ID( 'Projects' );
	wp_redirect( get_category_link( $cat_id ) ); exit;
}

//redirect to child if is archive for top-level parent
if ( is_category( $parent ) ) {
	first_subcat_redirect();
}

get_header(); ?>

<div id="content">
    <div class="wrap">
        <div class="column column-narrow">

<?php
echo "<p class='heading'>".$parent."</p>";
//list all categories, posts listed underneath, hide empty
list_cat_posts_all()
?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar: Archive Pages') ) : ?>&nbsp;<?php endif; ?>

        </div><!-- end .column-narrow -->

        <div class="column column-content column-double column-last">
            <div id="crumbs">
            <!--?php the_breadcrumb(); ?-->
<?php
$args = array(
    'prefix' => '<p>',
    'suffix' => '</p>',
    'title' => __( '', 'breadcrumbs-plus' ),
    'home' => __( 'Home', 'breadcrumbs-plus' ),
    'sep' => '»',
    'front_page' => false,
    'bold' => true,
    'show_blog' => false,
    'echo' => true,
    'singular_post_taxonomy' => 'category'
);

breadcrumbs_plus( $args );
?>
            </div>

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h1><?php single_cat_title(); ?></h1>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h1>Archive for: <?php single_tag_title(); ?></h1>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h1>Archive for <?php the_time('F jS, Y'); ?></h1>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h1>Archive for <?php the_time('F, Y'); ?></h1>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h1>Archive for <?php the_time('Y'); ?></h1>
<?php /* If this is an author archive */ } ?>

<h1><?php single_post_title(); ?></h1>
            <div class="posts">
<?php
wp_reset_query();

  $args = array(
			'category_name' => 'individual-projects',
			'orderby' => 'title',
			'order' => 'ASC'
		);

$wp_query = new WP_Query( $args );
$wp_query->get_posts();

$current = array();
$past = array();
if ( have_posts() ) {
    while ( have_posts() ) {
	the_post();
	$publication = get( 'publication_detail_link_to_publication' );
	$abstract = get( 'project_detail_abstract' );

	//present
        if( $publication ) {
			$faux_cat_post_id = get( 'details_project_category');
			$faux_cat_link = get_permalink( $faux_cat_post_id );
			$faux_cat_post = get_post( $faux_cat_post_id );
			$faux_cat = $faux_cat_post->post_title;
			$individual_project = $post->post_name;
			$faux_post_link = add_query_arg( 'project', $individual_project, $faux_cat_link );
			$start_yr = get( 'project_detail_start_year', 1, 1, true, $ID );
			$end_yr = get('project_detail_end_year', 1, 1, true, $ID);
			if ( $start_yr !== $end_yr ) { $years = $start_yr.'-'.$end_yr; } else { $years = $end_yr; }
			$meta_values = get_post_meta( $post->ID, 'publication_detail_link_to_publication' );
            sort( $meta_values );
			$publications[$end_yr][$post->post_title] = array( 'links'=>$meta_values, 'post-link'=>$faux_post_link,'faux-cat-name'=>$faux_cat,'faux-cat-link'=>$faux_cat_link, 'years'=>$years );
        }
    } //end while haveposts
	krsort( $publications, SORT_NUMERIC );
?>
<?php

        foreach ( $publications as $publication_details  ) {
            foreach ( $publication_details as $title=>$detail ) {
?>
            <div class='post'>

		<h2><?php echo $title; ?></a></h2>
        <div class="project-info">
        	<ul>
		<?php
		foreach ( $detail['links'] as $link ) {
		?>
		<li><a target="_blank" href="<?php echo $link; ?>" rel="" title=""><?php echo $link; ?></a></li>
        <?php 
		} //end foreach
		?>
        </ul>
        </div>
        <div class='project-tags'><?php echo "Filed under: <span><a title='View all projects under ".$detail['faux-cat-name']."' href='".$detail['faux-cat-link']."'>".$detail['faux-cat-name']."</a></span>"; ?> <span><?php echo $detail['years']; ?></span></div>
		<div class="clear">&nbsp;</div>

	</div><!--.post-->
<?php
	} //endforeach


		} //end if haveposts
}		else {
?>
            <h2>Sorry, there are no posts in this category.</h2>
<?php
}
?>
            <div class="navigation">
                <p><span class="older"><?php next_posts_link('&lt; older entries') ?></span>  <span class="latest"><?php previous_posts_link('newer entries &gt;') ?></span></p>
            </div>
	</div><!-- end .posts -->

    </div><!-- end .column-content -->

    <div class="clear">&nbsp;</div>

    </div><!-- end .wrap -->

</div><!-- end #content -->

<?php get_footer(); ?>
